#!python
# -*- coding: utf-8 -*-
import pymysql
from flask import Blueprint, request, render_template
from .db import get_db
from .alipay_parameters import TRADE_STATUS as TS
from .admin import login_required
order = Blueprint("order", __name__)

@order.route("/list")
@login_required
def order_list():
    start = int(request.values.get("start", 0))
    count = 20
    t = request.values.get("type", "")
    if not t:
        t = u"all"
    if t and t != "all":
        t1=u"""b.trade_status = '{status}'
        or b.refund_status = '{status}'
        """ .format(status =  pymysql.escape_string(t))
    else:
        t1 = "1"
    key = request.values.get("key", "")
    if key:
        t1 = u"""b.trade_no = '{key}'
        or b.receive_name = '{key}'
        or b.receive_phone  = '{key}'
        or b.receive_mobile = '{key}'
        or b.out_trade_no = '{key}'
        """.format(key = pymysql.escape_string(key))
    sql = u"""select
        b.trade_no,b.trade_status,b.receive_name,b.receive_address,b.receive_zip,
        b.receive_phone,b.receive_mobile, b.gmt_payment,p.productname, b.quantity, b.total_fee,
        t.trade_name, t.invoice_no, b.refund_status
    from online_buy b 
    left join trade_info t on b.trade_no = t.trade_no
    join product p on b.productno = p.productno
    where %s
        order by b.gmt_payment
    limit %d,%d
    """ % (t1, start, count)
    
        #where trade_status = 'WAIT_BUYER_SEND_GOODS'

    conn = get_db()
    cur = conn.cursor()
    cur.execute(sql)
    rs = cur.fetchall()
    cur.close()
    return render_template("order_list.html", items = rs, TS=TS, next= start+count, t = t)

